package cn.com.qingz.modules.user.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import cn.com.qingz.modules.user.model.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.miemiedev.mybatis.paginator.domain.PageList;

import cn.com.qingz.core.constantes.SystemConstantes;
import cn.com.qingz.core.exception.DicmfException;
import cn.com.qingz.core.model.DefaultResp;
import cn.com.qingz.modules.user.model.Brand;
import cn.com.qingz.modules.user.service.BrandService;
import cn.com.qingz.modules.user.vo.BrandSearchCondition;

/**
 * @author codeGen
 */
@Controller
@RequestMapping("brand/*")
public class BrandController {

    @Resource
    private BrandService brandService;

    /**
     * 保存品牌
     * @param
     * @return
     */
    @RequestMapping("save")
    @ResponseBody
    public DefaultResp<String> save(Brand entity) {
        DefaultResp<String> res = new DefaultResp<String>();
        try {
            brandService.doAddOrUpdate(entity);
        } catch (org.springframework.dao.DuplicateKeyException e) {
            throw new DicmfException(SystemConstantes.ERROR, "品牌编码已经存在");
        }
        return res;
    }

    /**
     * 分页查询
     * @param condition
     * @param page 页码
     * @return rows 每页行数
     */
    @RequestMapping("queryForPage")
    @ResponseBody
    public Map<String, Object> queryByPage(BrandSearchCondition condition, @RequestParam(required = false, defaultValue = "0") int page,
            @RequestParam(required = false, defaultValue = "10") int rows) {
        PageList<?> list = brandService.queryForPage(condition, page, rows);
        Map<String, Object> res = new HashMap<String, Object>();
        res.put("total", list.getPaginator().getTotalCount());
        res.put("rows", list);
        return res;
    }

    @RequestMapping("getBrandList")
    @ResponseBody
    public List<Brand> getBrandList() {
        List<Brand> list = brandService.queryAll();
        return list;
    }



    @RequestMapping("delete")
    @ResponseBody
    public DefaultResp<String> delete( Integer brandId) {
        DefaultResp<String> res = new DefaultResp<String>();
        brandService.doDelById(brandId);
        return res;
    }

}